package com.ruoyi.web.controller.activity.api;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.activity.service.IActivityService;
import com.ruoyi.common.annotation.Check;
import com.ruoyi.common.annotation.RequestLock;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.model.activity.Activity;
import com.ruoyi.model.activity.dto.ActivityDto;
import com.ruoyi.model.contest.Contest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author : 张怀秋
 **/
@Api(tags = "前台:活动接口")
@RestController
@RequestMapping("/api/activity")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ActivityApiController extends BaseController {

    private final IActivityService activityService;

    @ApiOperation("发布活动")
    @Check
    @PostMapping("/push/activity")
    @RequestLock(prefix = "pushActivity", expire = 1, timeUnit = TimeUnit.SECONDS)
    public AjaxResult pushActivity(@ApiParam("发布活动信息") @RequestBody ActivityDto activityDto) {
        return toAjax(activityService.pushActivity(activityDto));
    }

    @ApiOperation(value = "查询活动详情", notes = "并且返回对应的评论列表,并且增加对应的浏览量")
    @Check
    @GetMapping("/getActivityInfo/{id}/{type}")
    public AjaxResult getContestInfo(@ApiParam("活动Id") @PathVariable("id") String activityId,
                                     @ApiParam("类型") @PathVariable String type) {
        return success(this.activityService.getActivityInfoAndCommentList(activityId, type));
    }

    @ApiOperation(value = "首页:根据子分类查询活动", notes = "这是首页的分类所用到的接口没有分页的")
    @GetMapping("/queryActivityListByCategoryId/{categoryId}")
    public AjaxResult queryContestListByCategoryId(@PathVariable String categoryId) {
        return AjaxResult.success(this.activityService.list(Wrappers.<Activity>lambdaQuery()
                .eq(Activity::getCategoryId, categoryId)
                .eq(Activity::getStatus, "0")
                .orderByDesc(Activity::getViewNum)).stream().limit(8).collect(Collectors.toList()));
    }

    @ApiOperation("查询活动列表")
    @GetMapping("/activity-list/{page}/{limit}")
    public AjaxResult deskActivityList(@ApiParam("当前页码") @PathVariable Long page,
                                       @ApiParam("每页显示条数") @PathVariable Long limit,
                                       @ApiParam("分类id") String categoryId) {
        return AjaxResult.success(this.activityService.deskActivityList(page, limit, categoryId));
    }

    @ApiOperation(value = "当前最热门活动前10条", notes = "根据浏览量排序")
    @GetMapping("/activity-hotByOrder")
    public AjaxResult hotByOrderActivity() {
        return AjaxResult.success(this.activityService.list(Wrappers.<Activity>lambdaQuery()
                .eq(Activity::getStatus, "0")
                .orderByDesc(Activity::getViewNum).last("LIMIT 10")));
    }

    @ApiOperation(value = "当前最新活动前10条", notes = "根据发布时间排序")
    @GetMapping("/activity-newByOrder")
    public AjaxResult newByOrderActivity() {
        return AjaxResult.success(this.activityService.list(Wrappers.<Activity>lambdaQuery()
                .eq(Activity::getStatus, "0")
                .orderByDesc(Activity::getPushTime).last("LIMIT 10")));
    }

}
